﻿Public Class frmSprintsModify

    Dim strNameForm As String = "Modify Sprint"
    Dim mID As Integer

    Public Function Initialize(ByVal inID As Integer) As Boolean
        Try
            Me.Text = strNameForm
            mID = inID
            cboBacklogs.Load("Select * From Backlogs", "Name", gConnectionStringAgenda.ToString)

            AddHandler cboBacklogs.OnInsert, AddressOf InterfaceManager.Backlogs_OnInsert
            AddHandler cboBacklogs.OnModify, AddressOf InterfaceManager.Backlogs_OnModify

            If Not LoadForm() Then
                MsgBox("Impossible open form.", vbExclamation, Me.Text)
                Return False
            End If
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "LoadForm"

    Private Function LoadForm() As Boolean
        Try
            Dim oTSprints As New Data.TSprints
            If oTSprints.LoadData(mID) Then
                txtName.Text = oTSprints.mName
                txtDate.Data = oTSprints.mStartDate
                cboBacklogs.SelectedValue = oTSprints.mBacklogsID
            Else
                Return False
            End If
            oTSprints = Nothing
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Load data", Me.Name, "LoadForm", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModify.Click
        Try
            If IsComplete() Then
                Dim oTSprints As New Data.TSprints
                If oTSprints.LoadData(mID) Then
                    oTSprints.mName = txtName.Text
                    oTSprints.mStartDate = txtDate.DataMySql
                    oTSprints.mBacklogsID = cboBacklogs.SelectedValue

                    If oTSprints.Modify() Then
                        Me.DialogResult = Windows.Forms.DialogResult.OK
                    Else
                        MsgBox("Impossible modify the sprint.", vbExclamation, Me.Text)
                        Me.DialogResult = Windows.Forms.DialogResult.Cancel
                    End If
                Else
                    MsgBox("Impossible modify the sprint.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "cmdModify_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtName.Text = "" Then
                MsgBox("Insert name.", vbExclamation, Me.Name)
                txtName.Focus()
                Return False
            End If
            If Not cboBacklogs.IsItemSelected Then
                MsgBox("Select backlog.", vbExclamation, Me.Name)
                cboBacklogs.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub frmTableSingleInsertion_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class